package com.syh.jn.scm.domain.supplier.entity.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.jn.scm.domain.operation.entity.supplier.OperaSupplier;
import com.syh.jn.scm.domain.supplier.repository.base.StorehouseReadRepository;
import com.syh.jn.scm.domain.support.SuperBusinessAggregate;
import com.syh.jn.scm.dto.supplier.base.storeHouse.StorehouseMto;
import com.syh.jn.scm.dto.supplier.base.storeHouse.StorehouseSto;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.infrastructure.repository.impl.supplier.stock.StockRecordReadRepositoryMbImpl;
import com.syh.jn.scm.service.api.supplier.base.enums.StorehouseEnums;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class Storehouse extends SuperBusinessAggregate<Long> {
  /**
   * 创建仓库
   *
   * @param sto
   * @return
   */
  public void save(StorehouseSto sto) {
    ExtBeanUtils.copyProperties(sto, this);
    super.initDefault();
    this.status = StorehouseEnums.State.ENABLED.getStatus();
  }

  /**
   * 修改仓库
   * @param mto
   * @return
   */
  public void modify(StorehouseMto mto) {
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 删除仓库
   */
  public void destroy() {
    StockRecordReadRepositoryMbImpl stockRecordReadRepositoryMb = (StockRecordReadRepositoryMbImpl) SpringContextUtils.getBean(StockRecordReadRepositoryMbImpl.class);
    Wrapper we = Wrapper.build().append(Restrictions.eq("businessEntityId", this.getBusinessEntityId()))
            .append(Restrictions.eq("storehouseId", this.id));
    if (stockRecordReadRepositoryMb.getCount(we) > 0) {
      throw new BusinessException("该仓库已存在出入库记录，删除失败！");
    }
  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = StorehouseEnums.State.ENABLED.getStatus();
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = StorehouseEnums.State.DISABLED.getStatus();
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 创建直采直销仓库
   * @param supplier 需要创建仓库的供应商
   * @return
   */
  public static Storehouse createPurchaseToSaleStorehouse(OperaSupplier supplier) {
    StorehouseReadRepository storehouseReadRepository = (StorehouseReadRepository) SpringContextUtils.getBean("storehouseRepositoryImpl");
    Wrapper we = Wrapper.build().append(Restrictions.eq("businessEntityId", supplier.getId()))
            .append(Restrictions.eq("isPerchaseToSale", StorehouseEnums.IsPerchaseToSale.YES.getStatus()));
    if (storehouseReadRepository.getCount(we) > 0) {
      throw new BusinessException("该服务商已存在直采直销仓库，创建直采直销仓库失败");
    }
    Storehouse storehouse = new Storehouse();
    storehouse.initDefault();
    storehouse.setBusinessEntityId(supplier.getId());
    storehouse.setName("直采直销仓库");
    storehouse.setType(StorehouseEnums.Type.FICTITIOUS.getStatus());
    storehouse.setIsPerchaseToSale(StorehouseEnums.IsPerchaseToSale.YES.getStatus());
    storehouse.setStatus(StorehouseEnums.State.ENABLED.getStatus());
    storehouse.setArea(supplier.getArea());
    storehouse.setAddress(supplier.getAddress());
    //默认直采直销仓库由系统创建，操作员id填0
    storehouse.setOperatorId(0L);
    return storehouse;
  }

  /**
   * 仓库类型(1：实体仓库 2：逻辑仓库)
   */
  private Short type;
  /**
   * 仓库用途(例如 良品仓库 或者 非良品仓库 或者 赠品仓库 字典设置)
   */
  private Short purpose;
  /**
   * 状态:-1：停用 1：启用
   */
  private Short status;
  /**
   * 是否为直采直销仓 1：是
   */
  private Short isPerchaseToSale;
  /**
   * 名称
   */
  private String name;
  /**
   * 仓库编号
   */
  private String code;
  /**
   * 地址省 市 区
   */
  private String area;
  /**
   * 仓库地址
   */
  private String address;
  /**
   * 操作人员id
   */
  private Long operatorId;
}
